package com.hxh.generater.springBoot;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.hxh.config.ApplicationContextUtil;
import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.entity.ProjectDatasource;
import com.hxh.entity.ProjectTableFiledConfig;
import com.hxh.entity.ProjectTablePageConfig;
import com.hxh.entity.input.ProjectTablePageConfigInput;
import com.hxh.entity.po.ClassInfo;
import com.hxh.entity.po.FieldName;
import com.hxh.generater.springBoot.util.ConnetMysql;
import com.hxh.service.ProjectDatasourceService;
import com.hxh.service.ProjectTableFiledConfigService;
import com.hxh.service.ProjectTablePageConfigService;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater
 * @Author: HXH
 * @Description: 获取表结构
 * @Date: 2023/11/28 15:08
 */
public class GenerationTableInfo {

    /**
     * @param project      :
     * @param datasourceId
     * @param tableName    :         表名
     * @return com.hxh.entity.po.ClassInfo
     * @author HXH
     * @description:
     * @date 2023/11/28
     **/
    public static ClassInfo getTableInfo(Project project, Integer datasourceId, String tableName) throws SQLException {
        if (datasourceId == null) {
            datasourceId = project.getDatasourceId();
        }
        ProjectDatasourceService projectDatasourceService = ApplicationContextUtil.getOneBeanByType(ProjectDatasourceService.class);
        ProjectDatasource projectDatasource = projectDatasourceService.selectById(datasourceId);
        ConnetMysql connetMysql = new ConnetMysql();
        Connection connection = connetMysql.getConnection(projectDatasource);

        String table_schema = projectDatasource.getMysqlDatabaseName();

        Statement statement = connection.createStatement();
//        5.执行SQL的对象去执行SQL，返回结果集
        String sql = "show full columns from   `" + tableName + "` ;";
        ResultSet resultSet = statement.executeQuery(sql);
        List<FieldName> fieldNames = new ArrayList<>();
        ClassInfo classInfo = new ClassInfo();
        while (resultSet.next()) {
            FieldName fieldName = new FieldName().getOneByRs(resultSet);
            //获取表配置
            getProjectTableFiledConfig(project.getId(), datasourceId, fieldName.getName(), tableName, fieldName);
            fieldNames.add(fieldName);
        }


        classInfo.setFieldNameList(fieldNames);

        List<FieldName> idList = new ArrayList<>();
        for (FieldName fieldName : fieldNames) {
            if (Constants.MYSQL_PRIMARY_KEY_NAME.equals(fieldName.getKey())) {
                idList.add(fieldName);
            }
        }
        classInfo.setIdNameList(idList);

        sql = "SELECT table_comment FROM information_schema.TABLES WHERE table_name= '" + tableName + "' AND table_schema = '" + table_schema + "'";
        resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            classInfo.setDesc(resultSet.getString("TABLE_COMMENT"));
        }
        classInfo.setTableName(tableName);
        String className = StrUtil.toCamelCase(tableName);
        ;
        className = StringUtils.capitalize(className);
        classInfo.setClassName(className);
        classInfo.setProjectDatasource(projectDatasource);

        //获取页面配置
        getProjectTablePageConfig(project.getId(),datasourceId, tableName, classInfo);

//        6.释放连接
        resultSet.close();
        statement.close();
        connection.close();
        return classInfo;
    }

    public static void getProjectTablePageConfig(Integer projectId, Integer datasourceId, String tableName, ClassInfo classInfo) {
        ProjectTablePageConfigService projectTablePageConfigService = ApplicationContextUtil.getOneBeanByType(ProjectTablePageConfigService.class);
        ProjectTablePageConfigInput pojo = new ProjectTablePageConfigInput();
        pojo.setProjectId(projectId);
        pojo.setTableName(tableName);
        pojo.setDatasourceId(datasourceId);
        List<ProjectTablePageConfig> projectTablePageConfigList = projectTablePageConfigService.select(pojo);
        if (CollectionUtil.isNotEmpty(projectTablePageConfigList)) {
            classInfo.setProjectTablePageConfig(projectTablePageConfigList.get(0));
        } else {
            classInfo.setProjectTablePageConfig(ProjectTablePageConfig.generationDefaultByClassInfo(classInfo));
        }
    }


    /**
     * @param projectId:
     * @param filedName:
     * @param tableName:
     * @return com.hxh.entity.generation.po.ClassInfo
     * @author HXH
     * @description: 获取每个字段的映射配置
     * @date 2024/5/22
     **/
    public static void getProjectTableFiledConfig(Integer projectId, Integer datasourceId,
                                                  String filedName, String tableName, FieldName fieldName) {
        ProjectTableFiledConfigService projectTableFiledConfigService = ApplicationContextUtil.getOneBeanByType(ProjectTableFiledConfigService.class);

        List<ProjectTableFiledConfig> projectTableFiledConfig = projectTableFiledConfigService.selectByIdProjectIdTableNameFiledName(projectId, datasourceId, filedName, tableName);
        if (CollectionUtil.isNotEmpty(projectTableFiledConfig)) {
            fieldName.setProjectTableFiledConfig(projectTableFiledConfig.get(0));
        }
    }

}
